package edu.suda.xsap.controller;
import edu.suda.xsap.common.Result;
import edu.suda.xsap.dto.CustomerStatsDto;

import edu.suda.xsap.service.CustomerStatsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Month;

import java.time.Year;
import java.util.List;

@RestController
@RequestMapping("/customerstats")
@Slf4j
@Tags({@Tag(name = "增量与流失管理", description = "增量与流失管理")})
public class CustomerStatsController {
    @Autowired
    private CustomerStatsService customerStatsService;


    @Operation(summary = "增量年查询",description = "查询所有的用户年增量信息")
    @ApiResponses ({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "查询失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @GetMapping("/incrementy")
    public Result incrementy(@RequestParam("year1") Year year1, @RequestParam("year2")Year year2) {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<CustomerStatsDto> customerStatsDtoList = customerStatsService.searchNewByYear(year1, year2);
            //
            return Result.success(customerStatsDtoList);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }
    @Operation(summary = "增量天查询",description = "查询所有的用户天增量信息")
    @ApiResponses ({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "查询失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @GetMapping("/incrementd")//查询会员天增量信息
    public Result incrementm(@RequestParam("year") Year year, @RequestParam("month") Month month) {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<CustomerStatsDto> customerStatsDtoList = customerStatsService.searchNewByYearAndMonth(year, month);
            //
            return Result.success(customerStatsDtoList);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }
    @Operation(summary = "增量季度查询",description = "查询所有的用户季度增量信息")
    @ApiResponses ({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "查询失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @GetMapping("/incrementq")
    public Result incrementq(@RequestParam("year") Year year) {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<CustomerStatsDto> customerStatsDtoList = customerStatsService.searchNewByYearAndQuarter(year);
            //
            return Result.success(customerStatsDtoList);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

    @Operation(summary = "增量月查询",description = "查询所有的用户月增量信息")
    @ApiResponses ({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "查询失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @GetMapping("/incrementm")
    public Result incrementm(@RequestParam("year") Year year) {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<CustomerStatsDto> customerStatsDtoList = customerStatsService.searchNewByMonth(year);
            //
            return Result.success(customerStatsDtoList);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }
    
}
